<?php

namespace App\Admin\Controllers;

use App\Models\Bestsellers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\Button;
use App\Models\BestsellersCategories;

class BestsellersController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('畅销书榜');
            $content->description('当前推荐畅销书');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('畅销书榜');
            $content->description('当前推荐畅销书');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('畅销书榜');
            $content->description('添加畅销书');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Bestsellers::class, function (Grid $grid) {

            $grid->model()->whereHas('product', function ($query) {

            });
            $grid->disableExport();
            $grid->pid('书籍ID');
            $grid->extra('书名')->display(function($extra){
                return $extra['name'];
            });
            $grid->column('bestsellercategories.title','畅销书分类');
            $grid->model()->orderBy('sort','desc');
            $grid->created_at('添加时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $categories = BestsellersCategories::select('id','title')->get();
        
        if($categories->isEmpty()){
            return Admin::script(<<<EOT
                swal({
                      title: "至少先添加一条畅销书分类",
                      type: "warning",
                      showCancelButton: false,
                      confirmButtonColor: "#DD6B55",
                      confirmButtonText: "确认",
                      closeOnConfirm: false
                    },
                    function(){
                        window.location.href = '/admin/bestsellers'
                    });
EOT
);
            
        }else{
            foreach ($categories as $item){
                $items[$item->id] = $item->title;
            }
        }
        
        return Admin::form(Bestsellers::class, function (Form $form) use ($items){

            
            $form->text('pid','书籍ID')->help('填写已在产品管理发布的书籍ID');

            $form->button();
            $form->select('category','畅销书分类')->options($items)->rules('required');

            $form->embeds('extra', '书籍信息', function ($form) {
                $form->text('name','书籍名')->help('查询出来对应书籍ID的书籍名');
            });
            
            $form->display('created_at', '添加时间');
            $form->display('updated_at', '最近更新');
            
            $form->saving(function (Form $form){
                
            });
        });
    }
}
